//
//  GDNewsDetailsListController.m
//  爱新奇－味道
//
//  Created by 热带雨林 on 17/2/17.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDNewsDetailsListController.h"
#import "GDNetWorkTool.h"
#import "GDNewListModel.h"
#import "Masonry.h"
#import "GDNewsListCell.h"
#import "GDNewsDetailsWebController.h"


#import <YYModel.h>


@interface GDNewsDetailsListController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSArray <GDNewListModel *> *modelList;
@property(nonatomic, weak)UITableView *tableView;

@end

static NSString *nomalCellID = @"cell_ID";
static NSString *headCellID = @"head_cell_ID";
static NSString *bigCellID = @"big_cell_ID";
static NSString *thridCellID = @"thrid_cell_ID";

@implementation GDNewsDetailsListController

- (instancetype)initWithTid:(NSString *)tid withCurrentIndexPath:(NSInteger)indexP{
    if(self = [super initWithNibName:nil bundle:nil]){
        self.tid = tid;
        self.indexP = indexP;
    }
    return self;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self loadData];
    [self setupUI];
}
- (void)setupUI{
    
    UITableView *tableView = [[UITableView alloc]init];
    self.tableView = tableView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //取消分隔线
    tableView.separatorStyle = UITableViewCellEditingStyleNone;
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 46, 0);
    //自定义行高
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    //根据不同cell 注册不同的ID
    [tableView registerNib:[UINib nibWithNibName:@"GDNewsListCell" bundle:nil] forCellReuseIdentifier:nomalCellID];
    [tableView registerNib:[UINib nibWithNibName:@"GDHeadNewsCell" bundle:nil] forCellReuseIdentifier:headCellID];
    [tableView registerNib:[UINib nibWithNibName:@"GDBigNewsCell" bundle:nil] forCellReuseIdentifier:bigCellID];
    [tableView registerNib:[UINib nibWithNibName:@"GDNewsThridCell" bundle:nil] forCellReuseIdentifier:thridCellID];
    
}
- (NSInteger)tableView:(UITableView *)tableView     numberOfRowsInSection:(NSInteger)section{
    
    return self.modelList.count;
    
};
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GDNewListModel *model = self.modelList[indexPath.row];
    
    NSString *indentifier = nomalCellID;
    if(model.hasHead){
        
        indentifier = headCellID;
        
    }else if(model.imgType){
        
        indentifier = bigCellID;
        
    }else if(model.imgextra){
        
        indentifier = thridCellID;
        
    }
    
    GDNewsListCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    cell.model =  model;
    
    return  cell;
}

- (void)loadData{

    [[GDNetWorkTool shardNetWorkTool]getNewListWithTID:_tid complete:^(id respondOBJ) {
       //字典转模型
        NSArray <GDNewListModel *>*array = [NSArray yy_modelArrayWithClass:[GDNewListModel class] json:respondOBJ];
        
        self.modelList = array;
        
        //依靠网络加载的数据是需要获得 必须是需要刷新列表
        [self.tableView reloadData];
        
    }];

}
//点击某一个cell弹出 详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    GDNewListModel *model = self.modelList[indexPath.row];
    
    GDNewsDetailsWebController *webViewC = [[GDNewsDetailsWebController alloc]initWithDOCID:model.docid];
    [self.navigationController pushViewController:webViewC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
